package com.example.shoppingmallclient.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTabHost;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;


import com.example.shoppingmallclient.R;
import com.example.shoppingmallclient.fragment.FragmentFirst;
import com.example.shoppingmallclient.fragment.FragmentForth;
import com.example.shoppingmallclient.fragment.FragmentSecond;
import com.example.shoppingmallclient.fragment.FragmentThird;

public class TabHostActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_tabhost);

        //初始化TabHost
        initTabHost();

    }

    public void initTabHost(){

        //获取FragmentTabHost引用
        FragmentTabHost fragmentTabHost = findViewById(android.R.id.tabhost);

        //初始化
        fragmentTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

        //创建标签的TabSpec对象
        android.widget.TabHost.TabSpec tabSpec1 =fragmentTabHost.newTabSpec("tab1")
                .setIndicator(getTabSpecView("商品列表", R.drawable.productlist));
        android.widget.TabHost.TabSpec tabSpec2 =fragmentTabHost.newTabSpec("tab2")
                .setIndicator(getTabSpecView("推荐", R.drawable.discount));
        android.widget.TabHost.TabSpec tabSpec3 =fragmentTabHost.newTabSpec("tab3")
                .setIndicator(getTabSpecView("购物车", R.drawable.cart));
        android.widget.TabHost.TabSpec tabSpec4 =fragmentTabHost.newTabSpec("tab4")
                .setIndicator(getTabSpecView("我", R.drawable.me));

        fragmentTabHost.addTab(tabSpec1, FragmentFirst.class, null);
        fragmentTabHost.addTab(tabSpec2, FragmentSecond.class, null);
        fragmentTabHost.addTab(tabSpec3, FragmentThird.class, null);
        fragmentTabHost.addTab(tabSpec4, FragmentForth.class, null);
    }

    public View getTabSpecView(String title, int drawable){

        LayoutInflater inflater = getLayoutInflater();
        View view = inflater.inflate(R.layout.tabspec_layout, null, false);

        //获取ImageView和TextView的引用
        ImageView icon = view.findViewById(R.id.iv_icon);
        TextView textView = view.findViewById(R.id.tv_title);

        //设置控件显示的内容
        icon.setBackgroundResource(drawable);
        textView.setText(title);

        return view;

    }
}